//
//  DistinationMoreViewController.m
//  HeartTravel
//
//  Created by dlios on 15-7-2.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DistinationMoreViewController.h"
#import "MyCell.h"
#import "HTTPTool.h"
#import "UIImageView+WebCache.h"
#import "DestinationCover.h"
#import "PlaceViewController.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface DistinationMoreViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic, retain)NSMutableArray *arr;

@end

@implementation DistinationMoreViewController

- (void)dealloc{
    [_arr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createData];
    [self createView];
}

- (void)createData{
    self.arr = [NSMutableArray array];
    
    [HTTPTool get:[NSString stringWithFormat:@"http://api.breadtrip.com/destination/index_places/%ld/",self.index] body:nil httpResult:JSON success:^(id result) {
        NSDictionary *dic = result;
        self.title = dic[@"title"];
        NSArray *dataArr = dic[@"data"];
        for (NSDictionary *tempDic in dataArr) {
            DestinationCover *cover = [[DestinationCover alloc] initWithDic:tempDic];
            [self.arr addObject:cover];
            [cover release];
        }
        UICollectionView *collectionView = (UICollectionView *)[self.view viewWithTag:1000];
        [collectionView reloadData];
//        NSLog(@"%@",self.arr);
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (void)createView{
    //返回按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-gezicomfanhui1"] style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
    
    UICollectionViewFlowLayout *FlowLayout = [[UICollectionViewFlowLayout alloc] init];
    FlowLayout.itemSize = CGSizeMake((WIDTH - 30) / 2, (WIDTH - 30) / 2);
    FlowLayout.minimumLineSpacing = 10;
    FlowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 0, 10);
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 49 - 64) collectionViewLayout:FlowLayout];
    [self.view addSubview:collectionView];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [collectionView release];
    collectionView.backgroundColor = [UIColor whiteColor];
    
    [collectionView registerClass:[MyCell class] forCellWithReuseIdentifier:@"reuseMyCell"];
    collectionView.tag = 1000;
}

//返回按钮方法
- (void)back:(UIButton *)button{
    [self.navigationController popViewControllerAnimated:YES];
}

//collectionView两个必须执行的协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.arr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuseMyCell" forIndexPath:indexPath];
    DestinationCover *cover = self.arr[indexPath.row];
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:cover.cover]];
    cell.myLabel.text = cover.name;
    return cell;
}

//点击cell的方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    DestinationCover *cover = self.arr[indexPath.row];
    PlaceViewController *placeVC = [[PlaceViewController alloc] init];
    [self.navigationController pushViewController:placeVC animated:YES];
    placeVC.bId = (NSNumber *)cover.bId;
    placeVC.type = cover.type;
    [placeVC release];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
